﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FAS.Common.DomainModel;
using NHibernate.Criterion;

namespace FAS.Common.Repository.Nhb
{
    public class RepositoryNhb<T> : IRepository<T> where T : Entity
    {
        protected virtual ISession Session
        {
            get { return SessionBuilder.CreateSession(); }
        }
        #region IRepository<T> 成员
        public virtual T Load(string id)
        {
            try
            {
                T reslut = Session.Load<T>(id);
                if (reslut == null)
                    throw new RepositoryException("返回实体为空");
                else
                    return reslut;
            }
            catch (Exception ex)
            {
                throw new RepositoryException("获取实体失败", ex);
            }
        }

        public virtual T Get(string id)
        {
            try
            {
                T reslut = Session.Get<T>(id);
                if (reslut == null)
                    throw new RepositoryException("返回实体为空");
                else
                    return reslut;
            }
            catch (Exception ex)
            {
                throw new RepositoryException("获取实体失败", ex);
            }
        }

        public virtual IList<T> GetAll()
        {
            return Session.CreateCriteria<T>()
                .AddOrder(Order.Asc("CreateTime"))
                .List<T>();
        }

        public virtual void SaveOrUpdate(T entity)
        {
            try
            {
                Session.SaveOrUpdate(entity);
                Session.Flush();
            }
            catch (Exception ex)
            {
                throw new RepositoryException("插入实体失败", ex);
            }
        }

        public virtual void Update(T entity)
        {
            try
            {
                Session.Update(entity);
                Session.Flush();
            }
            catch (Exception ex)
            {
                throw new RepositoryException("更新实体失败", ex);
            }
        }

        public virtual void PhysicsDelete(string id)
        {
            try
            {
                var entity = Get(id);
                Session.Delete(entity);
                Session.Flush();
            }
            catch (System.Exception ex)
            {
                throw new RepositoryException("物理删除实体失败", ex);
            }
        }
        #endregion

        //判断字段的值是否存在 如果是插入id赋值-1或者new Guid,如果是修改id赋值 要修改项的值
        public virtual bool IsFieldExist(string fieldName, string fieldValue, string id, string where)
        {
            if (!string.IsNullOrEmpty(where))
                where = @" and " + where;
            var query = Session.CreateQuery(
                string.Format(@"select count(*) from {0} as o where o.{1}='{2}' and o.Id<>'{3}'" + where,
                typeof(T).Name,
                fieldName,
                fieldValue, id));

            return query.UniqueResult<long>() > 0;
        }
    }
}
